package com.brightworks.langcollabplayer.business
{
    import com.adobe.cairngorm.CairngormError;
    import com.adobe.cairngorm.CairngormMessageCodes;
    import com.brightworks.sql.SQLService;
    import com.brightworks.brightcairngorm.business.SQLServices;
    import com.universalmind.cairngorm.business.ServiceLocator;

    import flash.events.Event;
    import mx.core.Application;
    import mx.events.FlexEvent;

    public class ServiceLocator extends com.universalmind.cairngorm.business.ServiceLocator
    {
        private static var _instance:com.brightworks.langcollabplayer.business.ServiceLocator;
        protected var sqlServices:SQLServices;

        public function ServiceLocator()
        {
            if (_instance != null)
            {
                throw new CairngormError( CairngormMessageCodes.SINGLETON_EXCEPTION, "ServiceLocator" );
            }
            _instance = this;
            Application(Application.application).addEventListener(FlexEvent.PREINITIALIZE, preinitialize);
        }

        public static function getInstance():com.brightworks.langcollabplayer.business.ServiceLocator
        {
            if (_instance == null)
            {
                _instance = new com.brightworks.langcollabplayer.business.ServiceLocator();
            }
            return _instance;
        }

        /**
         * Retrieves the unique SWLService registered to the IAIRServiceLocator
         *
         * @param  the unique identifier of the SQLService
         * @return the SQLService instance with the specified name
         *
         */
        public function getSQLService(name:String) : SQLService
        {
           if (sqlServices == null)
           {
               sqlServices = new SQLServices();
               sqlServices.register( this );
           }
           return sqlServices.getService( name );
        }

        /**
         * Abstract method which is invoked when Application.preinitialize
         * event is dispatched.
         *
         * <p>
         * Typically, clients would override this method in order to initialize
         * services at startup of the application
         * </p>
         *
         * @param FlexEvent.PREINITIALIZE
         *
         */
        protected function preinitialize(event:FlexEvent) : void
        {
            throw new CairngormError(CairngormMessageCodes.ABSTRACT_METHOD_CALLED, "preinitialize");
        }
    }
}

class Private {}


